package com.wy.component.dataConvert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;

/**
 * 判断参数是否合法
 * 
 * @author FanHang
 * 
 */
public class ArgsUtils {

	public static void main(String[] args) {
		Object[] objects = new Object[] { "2014-08-05", "13" };
		System.out.print(getArrayAt(objects, 0));
	}

	public static Object getArrayAt(Object[] objects, Integer index) {
		try {
			Object returnObject = objects[index];
			if (returnObject == null) {
				return "";
			}
			return objects[index];
		} catch (Exception exc) {
			return "";
		}
	}

	/**
	 * 将列表转换成数组
	 * 
	 * @param collection
	 * @return
	 */
	public static T[] toArray(List<T> collection) {
		if (collection == null || collection.size() == 0) {
			return null;
		}
		return collection.toArray(new T[collection.size()]);
	}

	/**
	 * 将列表转换成数组
	 * 
	 * @param collection
	 * @return
	 */
	public static T[] toArray(Set<T> collection) {
		if (collection == null || collection.size() == 0) {
			return null;
		}
		return collection.toArray(new T[collection.size()]);
	}

	/**
	 * 比较Date大小
	 * 
	 * @param smallDate
	 *            小
	 * @param bigDate
	 *            大
	 * @return 如果正确为true，否则为false
	 */
	public static boolean compareDate(Date smallDate, Date bigDate) {
		if (smallDate == null || bigDate == null) {
			return false;
		}
		if (smallDate.getTime() <= bigDate.getTime()) {
			return true;
		}
		return false;
	}

	/**
	 * 与当前时间比较,较小的话就是true
	 * 
	 * @param smallDate
	 * @return
	 */
	public static boolean compareTo(Date smallDate) {
		try {
			if (Calendar.getInstance().getTime().compareTo(smallDate) == 1) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return true;
		}
	}

	/**
	 * 格式化日期，根据不同的格式
	 * 
	 * @param date
	 *            时间
	 * @param format
	 *            格式[yyyy][-MM][-dd] [HH][:mm][:ss]
	 * @return String
	 */
	public static String formatDate(Date date, String format) {
		if (date == null) {
			date = Calendar.getInstance().getTime();
		}
		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 转换成Date
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date parseDate(String dateStr, String format) {
		if (isEmpty(dateStr)) {
			Date nowDate = Calendar.getInstance().getTime();
			dateStr = new SimpleDateFormat(format).format(nowDate);
		}
		try {
			return new SimpleDateFormat(format).parse(dateStr);
		} catch (ParseException e) {
			Date nowDate = Calendar.getInstance().getTime();
			dateStr = new SimpleDateFormat(format).format(nowDate);
			try {
				return new SimpleDateFormat(format).parse(dateStr);
			} catch (ParseException e1) {
				return Calendar.getInstance().getTime();
			}
		}
	}

	/**
	 * 转换成Date
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date parseDate(String dateStr, String format, Date returnDate) {
		if (isEmpty(dateStr)) {
			return returnDate;
		}
		try {
			return new SimpleDateFormat(format).parse(dateStr);
		} catch (ParseException e) {
			return returnDate;
		}
	}

	/**
	 * 转换成Long
	 * 
	 * @param obj
	 * @return
	 */
	public static Long parseLong(Object obj) {
		try {
			if (obj == null)
				return 0l;
			return Long.parseLong(obj.toString());
		} catch (Exception exc) {
			return 0l;
		}
	}

	/**
	 * 转换成Integer
	 * 
	 * @param obj
	 * @return
	 */
	public static Integer parseInteger(Object obj) {
		try {
			if (obj == null)
				return 0;
			return Integer.parseInt(obj.toString());
		} catch (Exception exc) {
			return 0;
		}
	}

	/**
	 * 转换成Double
	 * 
	 * @param obj
	 * @return
	 */
	public static Double parseDouble(Object obj) {
		try {
			if (obj == null)
				return 0.0;
			return Double.parseDouble(obj.toString());
		} catch (Exception exc) {
			return 0.0;
		}
	}

	/**
	 * 给date增加天数
	 * 
	 * @param date
	 *            现在时间
	 * @param addDays
	 *            要增加几天,可以为正数/负数
	 * @return Date
	 */
	public static Date getDate(Date date, int addDays) {
		if (date == null) {
			date = Calendar.getInstance().getTime();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, addDays);
		return calendar.getTime();
	}

	/**
	 * 判断是否为空
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmpty(String args) {
		return StringUtils.isEmpty(args);
	}

	/**
	 * 判断是否为空
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmpty(Long args) {
		if (args == null) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为空
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmpty(Integer args) {
		if (args == null) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为空
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmpty(Double args) {
		if (args == null) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为NULL,或负数
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmptyOrMinus(Integer args) {
		if (args == null) {
			return true;
		}
		if (args < 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为NULL,或负数
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmptyOrMinus(Long args) {
		if (args == null) {
			return true;
		}
		if (args < 0L) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为NULL,或负数
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmptyOrMinus(Double args) {
		if (args == null) {
			return true;
		}
		if (args < 0L) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为NULL,或小于当前时间
	 * 
	 * @param args
	 * @return boolean
	 */
	public static boolean isEmptyOrMinus(Date args) {
		if (args == null) {
			return true;
		}
		try {
			if (args.getTime() <= Calendar.getInstance().getTimeInMillis()) {
				return true;
			}
		} catch (Exception exc) {
			// 时间参数存在问题
			exc.printStackTrace();
			return true;
		}
		return false;
	}

	/**
	 * 获取给定时间的第一天
	 */
	public static Date getFirstDate(Date nowMonth) {
		String year_month = formatDate(nowMonth, "yyyy-MM") + "-01";
		return parseDate(year_month, "yyyy-MM-dd");
	}

	/**
	 * 获取给定时间的最后一天
	 * 
	 * @param nowMonth
	 * @return
	 */
	public static Date getLastDate(Date nowMonth) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(nowMonth);
		final int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		String year_month = formatDate(nowMonth, "yyyy-MM") + "-" + String.valueOf(lastDay) + " 23:59:59";
		return parseDate(year_month, "yyyy-MM-dd HH:mm:ss");
	}

}
